<template>
  <li>
    <p class="title">
      <span>{{ num }}.</span>{{ title }}
    </p>
    <el-checkbox-group v-model="answer">
      <p v-for="(item, index) in option" :key="index">
        <el-checkbox :label="index" :disabled="type == 'readonly'">{{
          String.fromCharCode(65 + index) + ". " + item
        }}</el-checkbox>
      </p>
    </el-checkbox-group>
    <div v-if="type == 'readonly'" style="background-color: #f3f3f3;padding: 20px">
      <p>正确答案：{{data.trueAnswer}}</p>
      <p>你的答案：{{data.userAnswer}}</p>
      <p>描述：{{data.expound}}</p>
    </div>
    <el-divider></el-divider>
  </li>
</template>

<script>
export default {
  props: ["data", "num", "lastAnswer"],
  data() {
    return {
      // 将刷新前的数据进行赋值
      answer: [],
      type: this.$route.query.type,

    };
  },
  created () {
    if (this.lastAnswer == undefined || this.lastAnswer == null) {
      return;
    }
    let tempArr = this.lastAnswer.split("#");
    for (let i = 0; i < tempArr.length; i++) {
      for (let j = 0; j < this.option.length; j++) {
          if (tempArr[i] == this.option[j]) {
            this.answer[i] = j;
            break;
          }
      }
    }
  },
  watch: {
    answer: function (arr) {
      let tempArr = JSON.parse(JSON.stringify(arr))
      tempArr.sort()
      for (let i = 0; i < tempArr.length; i++) {
        tempArr[i] = this.option[tempArr[i]]
      }
      let element = ''
      for (let i = 0; i < tempArr.length; i++) {
        if (tempArr[i] == null || tempArr == undefined) {
          tempArr[i] = '';
        }
        if (i != 0) {
          element += '#' + tempArr[i]
        } else {
          element += tempArr[i]
        }
      }
      const temp = {
        index: Number(this.num) - 1,
        data: element,
      };
      this.$store.commit("saveAnswerMultipleChoice", temp);
    },
  },
  computed: {
    title() {
      return this.data.title.split("###")[0];
    },
    option() {
      let temp = this.data.title.split("###");
      return temp[1].split("#");
    },
  },
};
</script>

<style lang="scss" scoped>
li {
  list-style: none;
  .title {
    span {
      color: #63b1fe;
    }
  }
  p {
    padding-left: 15px;
    margin-bottom: 10px;
  }
}
</style>
